package com.ruoyi.web.controller.wexin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.weixin.anno.ILog;
import com.ruoyi.weixin.entity.Account;
import com.ruoyi.weixin.entity.AccountFam;
import com.ruoyi.weixin.entity.R;
import com.ruoyi.weixin.entity.User;
import com.ruoyi.weixin.service.AccountFamService;
import com.ruoyi.weixin.service.AccountsService;
import com.ruoyi.weixin.service.UserService;
import com.ruoyi.weixin.vo.AccountPararm;
import com.ruoyi.weixin.vo.Paihang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;

/**
 * (Account)表控制层
 *
 * @author makejava
 * @since 2024-01-02 17:19:56
 */

@RestController
@RequestMapping("/wexinLogin/famaccount")
public class AccountsFamController {
    /**
     * 服务对象
     */
    @Autowired
    UserService userService;

    @Autowired
    private AccountFamService accountService;

    @PostMapping
    public R selectAll(@RequestBody AccountPararm accountPararm) throws ParseException {
        return R.ok(accountService.accountList(accountPararm));
    }

    @PostMapping("selectPaihang")
    public R selectPaihang(@RequestBody Paihang paihang) {
        return R.ok(accountService.selectPaihang(paihang));
    }

    @PostMapping("yearAcount")
    public R yearAcount(@RequestBody Paihang paihang) {
        return R.ok(accountService.yearAcount(paihang));
    }


    @PostMapping("accountTbList")
    public R accountTbList(String type) {
        return R.ok(accountService.accountTbList(type));
    }

    @ILog("家庭账单添加/修改账单")
    @PostMapping("accountAdd")
    public R accountAdd(@RequestBody AccountFam accountFam) {
        System.err.println(accountFam);
        boolean b;
        if (accountFam.getId() != null) {
            accountFam.setUpdateTime(new Date());
            b = accountService.updateById(accountFam);
        } else {
            User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getId, accountFam.getUserId()));
            accountFam.setUserName(user.getNickName());
            b = accountService.save(accountFam);
        }
        return R.ok(b);
    }

    @ILog("删除账单")
    @PostMapping("accountDelete")
    public R accountDelete(String id) {
        return R.ok(accountService.removeById(id));
    }

    @PostMapping("accountUpdate")
    public R accountUpdate(String id) {
        return R.ok(accountService.accountUpdate(id));
    }

}

